﻿<cfsilent>
	<cfsetting requesttimeout="90" />
	<cfscript>

		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");

		chooseProp = getProperty("serviceFactory").getBean("choosePropertyAdvice");
		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		turnProp = getProperty("serviceFactory").getBean("turnPropertyAdvice");
		
		sequenceStudentCourse = getProperty("serviceFactory").getBean("sequenceStudentCourse");

		termId = event.getArg("TermID");
		courseId = event.getArg("CID");
		courseIdx = event.getArg("CIDX");
		taskType = event.getArg("TT");
		taskId = event.getArg("TSK");

		secureTicket = event.getArg("ST");
		
		turnId = event.getArg("CT");

		/* 学籍相关信息 */
		studentId = sessionAdvice.getAutherUserID();
		//studentClassId = sessionAdvice.getUserProp("studentClass");
		studentSubjectId = sessionAdvice.getUserProp("studentSubject");
		//studentDirId = sessionAdvice.getUserProp("studentSubDir");
		studentGrade = sessionAdvice.getUserProp("studentGrade");
		studentDepartment = sessionAdvice.getUserProp("studentDepartment");

		chooserSecure = false;
		chooserAllowed = false;

		/* 回执信息 */
		response = structNew();
		structInsert(response, "success", false, true);


		/* 首先做安全检查 */
		try {
			stringOrignal = decrypt(secureTicket, taskId, "PBEWithMD5AndDES", "Hex");
			stringTest = courseId & courseIdx & termId & taskType;
			
			if ( stringOrignal eq stringTest ) {
				chooserSecure = true;
			}
		}
		catch(any excpt) {
		}
		
		if ( chooserSecure ) {

			/* 检查选课轮次 */
			if ( len(turnId) ) {

				sql = "SELECT 
							date_start, date_end, time_start, time_end,
							grade_allowed, institute_allowed, subject_allowed, turn_prop 
						FROM 
							t_choose_turn 
						WHERE 
							ct = :turnId ";
				queryObj = new query( datasource=application.dnsSlave );
				queryObj.addParam( name="turnId", value=turnId, cfsqltype="cf_sql_char" );
				rs_turn = queryObj.execute( sql=sql ).getResult();
			
				if (rs_turn.recordCount GT 0) {
				
					today = datetimeAdvice.getCurrentDateString();
					currentTime = TimeFormat(now(), "HHMM");
				
					if ( rs_turn.date_start lte today and today lte rs_turn.date_end ) {
						if ( rs_turn.time_start lte currentTime and currentTime lte rs_turn.time_end ) {
							if ( rs_turn.grade_allowed eq "ALL" or listFind(rs_turn.grade_allowed, studentGrade, ",") ) {
								if ( rs_turn.institute_allowed eq "ALL" or listFind(rs_turn.institute_allowed , studentDepartment, ",") or (rs_turn.subject_allowed neq '-' and listFind(rs_turn.subject_allowed, studentSubjectId)) ) {
									/* 是否开放计划课程选课 */
									turnProp.parseProperty(rs_turn.turn_prop);
									if ( turnProp.getCommonTask() ) {
										chooserAllowed = true;
									}
								}
							}
						}
					}
				}
			}
			
			if ( chooserAllowed ) {
				
				transaction {
					
					/* 添加选课记录 */
					sql = "	SELECT 
								tsk_id, task_limite, task_chooesd 
							FROM 
								t_common_task 
							WHERE 
								tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name="taskId",value=taskId, cfsqltype="cf_sql_varchar");
					rs_task = queryObj.execute(sql=sql).getResult();

					/* 检查教学班是否还有空席位 */
					if ( rs_task.task_limite gt rs_task.task_chooesd ) {
						
						sql = "update t_common_task 
								SET task_chooesd = task_chooesd + 1  
								WHERE tsk_id = :taskId ";
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
						queryObj.execute( sql=sql );
			
						sequenceStudentCourse.setStudentID(studentId);
						sequenceStudentCourse.setCourseId(courseId);
						sequenceStudentCourse.setTermID(termId);
						sequenceStudentCourse.setTaskType(taskType);
						
						chooserId = sequenceStudentCourse.getID();
				
						timeStamp = datetimeAdvice.getCurrentDatetimeString();
						
						sql = "	SELECT
									tsc.scc_id,
									tsc.task_id,
									tsc.task_type,
									tsc.choose_prop
								FROM
									t_student_course tsc
								WHERE
									tsc.scc_id = :chooserId ";
									
						queryObj = new Query( datasource=application.dnsMaster );
						queryObj.addParam(name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
						rs_student_course = queryObj.execute( sql=sql ).getResult();
						
						if ( rs_student_course.recordCount EQ 0 ) {
							
							prop = chooseProp.init();
							
							/* 预置课表是否锁定 */
							prop.setLocked(false);
							/* 缴费状态设置 */
							prop.setPrePaid( true ); /* 是否从预交学费扣除 */
							prop.setPaid( true ); /* 是否已收费 */

							/* 没有选课记录 */
							sql = "INSERT INTO t_student_course
											  (scc_id,
											   task_type,
											   cidx,
											   task_id,
											   st,
											   retaking,
											   choose_time,
											   charge_time,
											   choose_prop,
											   term_id,
											   cid,
											   ct,
											   stu_id,
											   prop_id)
											VALUES
											 ( :chooserId ,
											   :taskType ,
											   :courseIdx ,
											   :taskId ,
											   :studyType ,
											   :retaking ,
											   :chooseTime ,
											   :chargeTime ,
											   :chooseProp ,
											   :termId ,
											   :courseId ,
											   :turnId ,
											   :studentId ,
											   '3' )";
							queryObj = new Query(datasource = application.dnsMaster);
							queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="courseIdx", value=courseIdx, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="studyType", value='N', cfsqltype="cf_sql_char" );
							queryObj.addParam( name="retaking", value='0', cfsqltype="cf_sql_char" );
							queryObj.addParam( name="chooseTime", value=timeStamp, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="chargeTime", value=timeStamp, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="chooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_char" );
							queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="turnId", value=turnId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
							queryObj.execute(sql = sql);
							
						}
						else {

							/* 更新选课记录 */
							prop = chooseProp.init();
							prop.parseProperty( rs_student_course.choose_prop );
							/* 缴费状态设置 */
							prop.setPrePaid( true ); /* 是否从预交学费扣除 */
							prop.setPaid( true ); /* 是否已收费 */

							//改选以后，将原来的教学任务人数实际人数减去1
							sql = "	UPDATE t_common_task
										SET
											task_chooesd = task_chooesd - 1 
										WHERE
											tsk_id = :taskId ";
							
							updateObj = new Query( datasource=application.dnsMaster );
							updateObj.addParam( name="taskId", value=rs_student_course.task_id, cfsqltype="cf_sql_varchar" );
							updateObj.execute( sql=sql );

							/*Unique: STU_ID, CID, TERM_ID, TASK_TYPE*/
							sql = "	UPDATE t_student_course tsc
										 SET 
											 tsc.choose_prop = :chooseProp ,
											 tsc.cidx = :courseIndex ,
											 tsc.task_id = :taskId ,
											 tsc.st = :studyType ,
											 tsc.retaking = :retaking ,
											 tsc.choose_time = :chooseTime ,
											 tsc.charge_time = :chargeTime ,
											 tsc.ct = :turnId ,
											 tsc.prop_id = :propId 
									WHERE
										 tsc.scc_id = :chooserId ";
							
							updateObj = new Query( datasource=application.dnsMaster );
							
							updateObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
							updateObj.addParam( name="propId", value="3", cfsqltype="cf_sql_char" );
							updateObj.addParam( name="turnId", value=turnId, cfsqltype="cf_sql_char" );
							updateObj.addParam( name="chargeTime", value=timeStamp, cfsqltype="cf_sql_char" );
							updateObj.addParam( name="chooseTime", value=timeStamp, cfsqltype="cf_sql_char" );
							updateObj.addParam( name="retaking", value="0", cfsqltype="cf_sql_char" );
							updateObj.addParam( name="studyType", value="N", cfsqltype="cf_sql_char" );
							updateObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
							updateObj.addParam( name="courseIndex", value=courseIdx, cfsqltype="cf_sql_char" );
							updateObj.addParam( name="chooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_varchar" );
							
							updateObj.execute(sql=sql);

						}

						structInsert(response, "success", true, true);
						
					}
					else {
						structInsert(response, "reason", "选课人数已满", true);
					}
					
				}
			}
			else {
				structInsert(response, "reason", "现在不是选课时段", true);
			}
		}
		else {
			structInsert(response, "reason", "参数错误", true);
		}

	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>